package com.wandian.util;

import java.util.Arrays;

/**
 * @author long
 * @Create 2012-10-5 上午10:35:59
 * @QQ 108232706
 * @version 1.0
 * @description ArrayList
 */
public class ArrayList<E> {
	/** 数组元素 */
	private transient Object[] elementData;
	/** 当前长度 */
	private int size;

	/** 支持是否可以扩容 */
	private boolean canExpand;

	private boolean showThread;
	// 处理空闲资源，无法在泛型里new，空指针
	// private ArrayList<E> freeList;

	// 打印线程资源抢占情况
	protected transient int modCount = 0;

	private transient String threadName;

	private final String listName;

	/**
	 * 根据容量，初始化一个类
	 * 
	 * @param initialCapacity
	 */
	public ArrayList(String _listName, int initialCapacity, boolean _canExpand, boolean _showThread) {
		super();
		synchronized (this) {
			if (initialCapacity < 0)
				throw new IllegalArgumentException("Illegal Capacity: " +
						initialCapacity);
			this.listName = _listName;
			this.elementData = new Object[initialCapacity];
			this.canExpand = _canExpand;
			this.showThread = _showThread;
			// for (int i = 0; i < initialCapacity; i++) {
			// freeList.add(null);
			// }
		}
	}

	public ArrayList(String _listName) {
		this(_listName, 10, true, false);
	}

	public ArrayList() {
		this("", 10, true, false);
	}

	public boolean add(E e) {
		synchronized (this) {
			printThread("add", true);
			if (canExpand) {
				ensureCapacity(size + 1); // Increments modCount!!
			}
			else {
				// 长度已经饱和，非扩展情况下，返回false
				if (size >= elementData.length) {
					printThread("add", false);
					return false;
				}
			}
			elementData[size++] = e;
			printThread("add", false);
			return true;
		}

	}

	private void printThread(String meathodName, boolean isStart) {
		if (showThread) {
			threadName = Thread.currentThread().getName();
			if (isStart)
				System.out.println("List:" + listName + " thread:" + threadName + ",<< " + meathodName);
			else
				System.out.println("List:" + listName + " thread:" + threadName + ", " + meathodName + ">>");
		}
	}

	/**
	 * 根据索引返回某个元素
	 * 
	 * @param index
	 * @return
	 */
	public E get(int index) {
		synchronized (this) {
			if (!RangeCheck(index))
				return null;
			return (E) elementData[index];

		}
	}

	/** 清空列表 */
	public void clear() {
		synchronized (this) {
			modCount++;
			for (int i = 0; i < size; i++) {
				// 回收数据
				// freeList.add((E) elementData[i]);
				elementData[i] = null;
			}
			size = 0;
		}
	}

	/**
	 * 根据索引删除一个元素
	 * 
	 * @param index
	 * @return
	 */
	public E remove(int index) {
		synchronized (this) {
			printThread("remove", true);
			if (!RangeCheck(index)) {
				printThread("remove", false);
				return null;
			}
			modCount++;
			E oldValue = (E) elementData[index];

			int numMoved = size - index - 1;
			if (numMoved > 0)
				System.arraycopy(elementData, index + 1, elementData, index,
						numMoved);
			elementData[--size] = null;
			printThread("remove", false);
			return oldValue;
		}
	}

	/**
	 * 数组里移除某个元素
	 * 
	 * @param o
	 * @return
	 */
	public boolean remove(Object o) {
		synchronized (this) {
			if (o == null) {
				for (int index = 0; index < size; index++)
					if (elementData[index] == null) {
						fastRemove(index);
						return true;
					}
			} else {
				for (int index = 0; index < size; index++)
					if (o.equals(elementData[index])) {
						fastRemove(index);
						return true;
					}
			}
			return false;
		}
	}

	private void fastRemove(int index) {
		modCount++;
		int numMoved = size - index - 1;
		if (numMoved > 0)
			System.arraycopy(elementData, index + 1, elementData, index,
					numMoved);
		elementData[--size] = null; // Let gc do its work
	}

	// public boolean isFreeListCorrect() {
	// synchronized (this) {
	// return (freeList.size() + this.size()) == elementData.length;
	// }
	// }

	private boolean RangeCheck(int index) {
		if (index >= size)
			return false;
		return true;
		// throw new IndexOutOfBoundsException(
		// "Index: " + index + ", Size: " + size);
	}

	/**
	 * 返回当前使用容量
	 * 
	 * @return
	 */
	public int size() {
		synchronized (this) {
			return size;
		}

	}

	/**
	 * 判断列表是否为空
	 * 
	 * @return
	 */
	public boolean isEmpty() {
		synchronized (this) {
			return size == 0;
		}

	}

	/**
	 * 判断数组是否包含某个元素
	 * 
	 * @param o
	 * @return
	 */
	public boolean contains(Object o) {
		synchronized (this) {
			return indexOf(o) >= 0;
		}
	}

	/**
	 * 取指定元素的索引
	 * 
	 * @param o
	 * @return
	 */
	public int indexOf(Object o) {
		synchronized (this) {
			if (o == null) {
				for (int i = 0; i < size; i++)
					if (elementData[i] == null)
						return i;
			} else {
				for (int i = 0; i < size; i++)
					if (o.equals(elementData[i]))
						return i;
			}
			return -1;
		}
	}

	/**
	 * 返回当前数组中有效元素的数组
	 * 
	 * @return
	 */
	public Object[] toArray() {
		synchronized (this) {
			return Arrays.copyOf(elementData, size);
		}
	}

	/**
	 * 最后面的开始计算的索引
	 * 
	 * @param o
	 * @return
	 */
	public int lastIndexOf(Object o) {
		synchronized (this) {
			if (o == null) {
				for (int i = size - 1; i >= 0; i--)
					if (elementData[i] == null)
						return i;
			} else {
				for (int i = size - 1; i >= 0; i--)
					if (o.equals(elementData[i]))
						return i;
			}
			return -1;
		}
	}

	/**
	 * 在扩展的情况下，系统根据最小需要去扩展数组
	 * 
	 * @param minCapacity
	 */
	private void ensureCapacity(int minCapacity) {
		modCount++;
		int oldCapacity = elementData.length;
		if (minCapacity > oldCapacity) {
			Object oldData[] = elementData;
			int newCapacity = (oldCapacity * 3) / 2 + 1;
			if (newCapacity < minCapacity)
				newCapacity = minCapacity;
			// minCapacity is usually close to size, so this is a win:
			elementData = Arrays.copyOf(elementData, newCapacity);
		}
	}

}
